package com.example.ordersystem.controller;

import com.example.ordersystem.entity.Order;
import com.example.ordersystem.entity.vo.FoodVo;
import com.example.ordersystem.entity.vo.OrdersFoodVo;
import com.example.ordersystem.entity.vo.ResultMessage;
import com.example.ordersystem.service.OrderService;
import com.example.ordersystem.util.DateUtil;
import com.example.ordersystem.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("order/orderService")
@CrossOrigin
public class OrderController {

    @Autowired
    OrderService orderService;

    @Autowired
    ResultUtil resultUtil;
    /**
     * 用户下单
     */
    @PutMapping("add")
    public ResultMessage add(@RequestBody OrdersFoodVo ordersFoodVo)
    {
       List<FoodVo> foodVoArrayList=ordersFoodVo.getFoodList();
       Order order=new Order();
       order.setUserId(ordersFoodVo.getUserId());
       order.setTel(ordersFoodVo.getTel());
       order.setCreateTime(DateUtil.getDate());
       order.setPrice(ordersFoodVo.getPrice());
       order.setRemarks(ordersFoodVo.getRemarks());
       order.setProgress(ordersFoodVo.getProgress());
       order.setAddress(ordersFoodVo.getAddress());
       order.setPayment(ordersFoodVo.getPayment());
       order.setToolNum(ordersFoodVo.getToolNum());
        boolean add = orderService.add(foodVoArrayList, order);
        if(add)
        {
            return resultUtil.success("下单成功");
        }else{
            return resultUtil.error("库存不足");
        }
    }

    /**
     * 用户查看自己的全部订单
     */
    @GetMapping("view/{id}/{currentPage}")
    public ResultMessage viewMine(@PathVariable String id, @PathVariable String currentPage)
    {
        return resultUtil.data(orderService.viewUserList(id,currentPage));
    }
    /**
     * 根据订单id查询订单详细信息
     */
    @GetMapping("viewDetail/{orderId}")
    public ResultMessage viewDetail(@PathVariable String orderId)
    {
         return  resultUtil.data(orderService.viewDetail(orderId));
    }

    /**
     * 配送员查看所有待接收订单
     */
    @GetMapping("viewAll")
    public ResultMessage viewAll()
    {
        return resultUtil.data(orderService.viewAll());
    }
    /**
     * 配送员查看自己的配送记录
     */
    @GetMapping("viewHandleList/{courierId}")
    public ResultMessage viewHandleList(@PathVariable String courierId)
    {
         return resultUtil.data(orderService.viewHandleList(courierId));
    }
    /**
     * 配送员查看自己已接收未处理的订单
     */
    @GetMapping("viewReceiveList/{courierId}")
    public ResultMessage viewReceiveList(@PathVariable String courierId)
    {
         return resultUtil.data(orderService.viewReceiveList(courierId));
    }
    /**
     * 配送员接收订单
     */
    @PostMapping("receive")
    public ResultMessage receive(@RequestBody Map<String,String>map)
    {
        orderService.receiveOrder(map.get("orderId"),map.get("courierId"));
        return resultUtil.success("接收成功");
    }
    /**
     * 配送员处理订单
     */
    @PostMapping("handle")
    public ResultMessage handle(@RequestBody Map<String,String> map)
    {
        orderService.handle(map.get("orderId"),DateUtil.getDate());
        return resultUtil.success("处理成功");
    }
    /**
     * 餐厅管理员查看所有订单
     */
    @GetMapping("admin/viewAll/{currentPage}")
    public ResultMessage adminViewAll(@PathVariable String currentPage)
    {
        return resultUtil.data(orderService.adminViewAll(currentPage));
    }
}
